/**
 * Copyright (C) 2017-2018 pinganfang, Inc. All Rights Reserved.
 */

package com.pingan.haofang.searchcloud.solr.common;

import java.util.regex.Pattern;

/**
 * 
 * @author huhailiang937
 * @date 2017年5月5日上午10:53:25
 * @since 1.0.0
 */
public class CamelNameUtils {
    
    private static Pattern REGEX = Pattern.compile("([A-Z][a-z]+)");
    
    private static String R = "$1_";
    
    /**
     * convert camel name to underscore name
     * 
     * @return
     */
    public static String camel2underscore(String camelName) {
        // 先把第一个字母大写
        String camelNameCapitalize = capitalize(camelName);
        String underscoreName = REGEX.matcher(camelNameCapitalize).replaceAll(R);
        // output: Pur_Order_Id_ 接下来把最后一个_去掉，然后全部改小写
        underscoreName = underscoreName.toLowerCase().substring(0, underscoreName.length() - 1);
        return underscoreName;
    }

    /**
     * convert underscore name to camel name
     * 
     * @param underscoreName
     * @return
     */
    public static String underscore2camel(String underscoreName) {
        String[] sections = underscoreName.split("_");
        StringBuilder sb = new StringBuilder(underscoreName.length());
        for (int i = 0; i < sections.length; i++) {
            String s = sections[i];
            if (i == 0) {
                sb.append(s);
            } else {
                sb.append(capitalize(s));
            }
        }
        return sb.toString();
    }

    /**
     * capitalize the first character
     * 
     * @param str
     * @return
     */
    public static String capitalize(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return str;
        }
        return new StringBuilder(strLen).append(Character.toTitleCase(str.charAt(0))).append(str.substring(1))
                .toString();
    }
}
